package imsdk

import (
	"encoding/json"
	"longmen/server/config/global"
	"longmen/server/pkg/common/helper/http"
)

type (
	GetOnlineNumReq struct {
		GroupId string `json:"GroupId"`
	}

	GetOnlineNumResp struct {
		ActionStatus    string `json:"ActionStatus"`
		ErrorCode       int    `json:"ErrorCode"`
		ErrorInfo       string `json:"ErrorInfo"`
		OnlineMemberNum int    `json:"OnlineMemberNum"`
	}
)

//https://console.tim.qq.com/v4/group_open_http_svc/get_online_member_num?sdkappid=88888888&identifier=admin&usersig=xxx&random=99999999&contenttype=json

func (s sdk) GetOnlineNum(req GetOnlineNumReq) *GetOnlineNumResp {
	reqUrl := s.BuildRestfulUrl("//group_open_http_svc/get_online_member_num")
	bytes, err := json.Marshal(req)
	if err != nil {
		global.Log.Error("GetOnlineNum error: ", err)
		return nil
	}

	result, err := http.HttpPost(reqUrl, string(bytes), connTimeoutMs, serveTimeoutMs, "application/json")

	resp := &GetOnlineNumResp{}
	err = json.Unmarshal(result, resp)

	if err != nil {
		global.Log.Error("GetOnlineNum Unmarshal error: ", err)
		return nil
	}
	if resp.ActionStatus == FailActionStatus {
		global.Log.Error("GetOnlineNum error: ", err, "error message: ", resp.ErrorInfo)
	}

	return resp

}
